package com.titleevaluation.controller;

import com.titleevaluation.entity.SocialInsuranceProof;
import com.titleevaluation.service.SocialInsuranceProofService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/applications/{applicationId}/social-insurance-proofs")
public class SocialInsuranceProofController {

    private final SocialInsuranceProofService socialInsuranceProofService;

    public SocialInsuranceProofController(SocialInsuranceProofService socialInsuranceProofService) {
        this.socialInsuranceProofService = socialInsuranceProofService;
    }

    @PostMapping
    public ResponseEntity<SocialInsuranceProof> createSocialInsuranceProof(@PathVariable Long applicationId, @RequestBody SocialInsuranceProof socialInsuranceProof) {
        SocialInsuranceProof createdSocialInsuranceProof = socialInsuranceProofService.createSocialInsuranceProof(applicationId, socialInsuranceProof);
        return ResponseEntity.ok(createdSocialInsuranceProof);
    }

    @GetMapping
    public ResponseEntity<List<SocialInsuranceProof>> getSocialInsuranceProofsByApplicationId(@PathVariable Long applicationId) {
        List<SocialInsuranceProof> socialInsuranceProofs = socialInsuranceProofService.getSocialInsuranceProofByApplicationId(applicationId);
        return ResponseEntity.ok(socialInsuranceProofs);
    }

    @GetMapping("/{id}")
    public ResponseEntity<SocialInsuranceProof> getSocialInsuranceProofById(@PathVariable Long id) {
        return socialInsuranceProofService.getSocialInsuranceProofById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PutMapping("/{id}")
    public ResponseEntity<SocialInsuranceProof> updateSocialInsuranceProof(@PathVariable Long id, @RequestBody SocialInsuranceProof socialInsuranceProof) {
        SocialInsuranceProof updatedSocialInsuranceProof = socialInsuranceProofService.updateSocialInsuranceProof(id, socialInsuranceProof);
        return ResponseEntity.ok(updatedSocialInsuranceProof);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteSocialInsuranceProof(@PathVariable Long id) {
        socialInsuranceProofService.deleteSocialInsuranceProof(id);
        return ResponseEntity.noContent().build();
    }
}

